package com.erp.programmer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.erp.programmer.bean.CodeMsg;
import com.erp.programmer.dao.SellCompanyMapper;
import com.erp.programmer.domain.SellCompany;
import com.erp.programmer.dto.PageDto;
import com.erp.programmer.dto.ResponseDto;
import com.erp.programmer.dto.SellCompanyDto;
import com.erp.programmer.service.ISellCompanyService;
import com.erp.programmer.utils.CommonUtil;
import com.erp.programmer.utils.CopyUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Takagi3
 * @since 2021-05-23
 */
@Service
public class SellCompanyServiceImpl extends ServiceImpl<SellCompanyMapper, SellCompany> implements ISellCompanyService {

    @Autowired
    SellCompanyMapper SellCompanyMapper;
    @Autowired
    ISellCompanyService iSellCompanyService;
    
    @Override
    public  ResponseDto<List<SellCompanyDto>> getData( ) {
        QueryWrapper<SellCompany> queryWrapper = new QueryWrapper<>();
        List<SellCompany> SellCompaniesListDb = SellCompanyMapper.selectList(queryWrapper);
        List<SellCompanyDto> SellCompanyDtoList = CopyUtil.copyList(SellCompaniesListDb, SellCompanyDto.class);
        return ResponseDto.success(SellCompanyDtoList);
    }

    /**
     * 分页
     * 搜索获取全部卖方公司信息
     */
    public ResponseDto<PageDto<SellCompanyDto>> getAll( PageDto<SellCompanyDto> pageDto) {
        QueryWrapper<SellCompany> queryWrapper = new QueryWrapper<>();
        if("name".equals(pageDto.getSearchType()) && !CommonUtil.isEmpty(pageDto.getSearchContent())){
            // 如果是根据角色名称搜索
            queryWrapper.like("name",pageDto.getSearchContent());
        }
        if(pageDto.getPage() == null){
            pageDto.setPage(1);
        }
        pageDto.setSize(5);
        PageHelper.startPage(pageDto.getPage(), pageDto.getSize());
        List<SellCompany> list = iSellCompanyService.list(queryWrapper);
        List<SellCompanyDto> sellCompanyDtos = CopyUtil.copyList(list, SellCompanyDto.class);
        PageInfo<SellCompany> pageInfo = new PageInfo<>(list);
        pageDto.setTotal(pageInfo.getTotal());
        pageDto.setList(sellCompanyDtos);
        return ResponseDto.success(pageDto);
    }

    /**
     *保存、修改卖方公司信息
     */
    public ResponseDto<PageDto<SellCompanyDto>> saveSellCompany( SellCompanyDto sellCompanyDto) {


        SellCompany sellCompany = CopyUtil.copy(sellCompanyDto, SellCompany.class);
        if(CommonUtil.isEmpty(sellCompany.getId())){
            //插入
            if (!iSellCompanyService.save(sellCompany)) {
                return ResponseDto.errorByMsg(CodeMsg.USER_ADD_ERROR);
            }
            PageDto<SellCompanyDto> pageDtoList = getAll(new PageDto<SellCompanyDto>()).getData();
            ResponseDto<PageDto<SellCompanyDto>> responseDto = getAll(pageDtoList);
            responseDto.setMsg("添加成功！");
            return responseDto;
        }else{
            //修改
            if (!iSellCompanyService.updateById(sellCompany)) {
                return ResponseDto.errorByMsg(CodeMsg.USER_EDIT_ERROR);
            }
            PageDto<SellCompanyDto> pageDtoList = getAll(new PageDto<SellCompanyDto>()).getData();
            ResponseDto<PageDto<SellCompanyDto>> responseDto = getAll(pageDtoList);
            responseDto.setMsg("修改成功！");
            return responseDto;
        }
    }

    /**
     * 分页
     *删除卖方公司信息
     */

    public ResponseDto<PageDto<SellCompanyDto>> deleteSellCompany( SellCompanyDto sellCompanyDto) {

        if(!iSellCompanyService.removeById(sellCompanyDto.getId())) {
            return ResponseDto.errorByMsg(CodeMsg.USER_DELETE_ERROR);
        }
        PageDto<SellCompanyDto> pageDtoList = getAll(new PageDto<SellCompanyDto>()).getData();
        ResponseDto<PageDto<SellCompanyDto>> responseDto = getAll(pageDtoList);
        responseDto.setMsg("删除成功！");
        return responseDto;
    }

    @Override
    public ResponseDto<List<SellCompanyDto>> add(SellCompanyDto SellCompanyDto)
    {
        // 2.转换
        SellCompany SellCompany = CopyUtil.copy(SellCompanyDto, SellCompany.class);
        // 3.业务逻辑
        SellCompanyMapper.insert(SellCompany);
        //return ResponseDto.errorByMsg(CodeMsg.USER_PASSWD_EDIT_ERROR);



        return getData();
    }

    @Override
    public ResponseDto<List<SellCompanyDto>> delete(SellCompanyDto SellCompanyDto) {
        // 2.转换
        SellCompany SellCompany = CopyUtil.copy(SellCompanyDto, SellCompany.class);
        // 3.业务逻辑
        SellCompanyMapper.deleteById(SellCompany.getId());
        //return ResponseDto.errorByMsg(CodeMsg.USER_PASSWD_EDIT_ERROR);



        return getData();
    }

    @Override
    public ResponseDto<List<SellCompanyDto>> edit(SellCompanyDto SellCompanyDto) {
        // 2.转换
        SellCompany SellCompany = CopyUtil.copy(SellCompanyDto, SellCompany.class);
        // 3.业务逻辑
        SellCompanyMapper.updateById(SellCompany);
        //return ResponseDto.errorByMsg(CodeMsg.USER_PASSWD_EDIT_ERROR);



        return getData();
    }


    @Override
    public ResponseDto<List<SellCompanyDto>> search(SellCompanyDto SellCompanyDto) {

        // 1.转换
        SellCompany SellCompany = CopyUtil.copy(SellCompanyDto, SellCompany.class);
        // 2.业务逻辑
        List<SellCompanyDto> SellCompanyDtoList = new ArrayList<>();
        QueryWrapper<SellCompany> queryWrapper = new QueryWrapper<>();
        if(!CommonUtil.isEmpty(SellCompanyDto.getSearchType()) && !CommonUtil.isEmpty(SellCompanyDto.getSearchContent())){
            // 用户点击了搜索
            if("name".equals(SellCompanyDto.getSearchType())){
                queryWrapper.like("name", SellCompanyDto.getSearchContent());
            }
        }
        List<SellCompany> SellCompaniesListDb = SellCompanyMapper.selectList(queryWrapper);
        SellCompanyDtoList = CopyUtil.copyList(SellCompaniesListDb, SellCompanyDto.class);

        return ResponseDto.success(SellCompanyDtoList);
    }
}
